using System;
using System.Collections.Generic;
using System.Linq;
using VMDS.Business.Entities;
using VMDS.Data.SQL;

namespace VMDS.Data
{
    public class VmSqlAdapter : ICrudEntity<VM>
    {
        private ModelContainer _db;
        void Validate(VM obj)
        {
            if (String.IsNullOrWhiteSpace(obj.Name)) throw new ArgumentNullException("obj", "Name is null");
            if (String.IsNullOrWhiteSpace(obj.CreatedBy)) throw new ArgumentNullException("obj", "CreatedBy is null");
            if (obj.Date == null) throw new ArgumentNullException("obj", "Date is null");

        }

        void Assign(VM old,VM obj)
        {
            old.Name = obj.Name;
            old.CreatedBy = obj.CreatedBy;
            old.Description = obj.Description;
            old.OSId = obj.OSId;
            old.SubjectId = obj.SubjectId;
            old.Version = obj.Version;
          //  old.Storages = obj.Storages;
          //  old.ScreenShots = obj.ScreenShots;
        }

        public ICollection<VM> GetAll()
        {
            return new List<VM>(_db.VMs);
        }

        public VM GetById(int id)
        {
            return _db.VMs.SingleOrDefault(x => x.Id == id);
        }

        public void Update(VM obj)
        {
            Validate(obj);
            VM old = GetById(obj.Id);
            Assign(old, obj);

        }

        public void Delete(VM obj)
        {
           
        }

        public void Create(VM obj)
        {
            Validate(obj);
            VM old = _db.VMs.CreateObject();
            Assign(old, obj);
        }
        public VmSqlAdapter(ModelContainer db)
        {
            _db = db;
        }
    }
}